package io.github.talelin.latticy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.dto.BannerDTO;
import io.github.talelin.latticy.mapper.BannerItemMapper;
import io.github.talelin.latticy.mapper.BannerMapper;
import io.github.talelin.latticy.model.BannerDO;
import io.github.talelin.latticy.model.BannerItemDO;
import io.github.talelin.latticy.vo.BannerWithItemsBO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName BannerService
 * @Description TODO
 * @Author wushaopei
 * @Date 2021/10/17 20:44
 * @Version 1.0
 */
@Service
public class BannerService extends ServiceImpl<BannerMapper,BannerDO>{

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private BannerItemMapper bannerItemMapper;

    public BannerWithItemsBO getWithItem(Long id){

        BannerDO bannerDO = bannerMapper.selectById(id);
        if(bannerDO == null){
            new NotFoundException(20000);
        }

        QueryWrapper<BannerItemDO> wrapper = new QueryWrapper();
        //
//        wrapper.eq("banner_id",id);
        wrapper.lambda().eq(BannerItemDO::getBannerId,id);
        List<BannerItemDO> bannerItemDOList =  bannerItemMapper.selectList(wrapper);
        // 链式调用
        /*List<BannerItemDO> bannerItemDOList = new LambdaQueryWrapper<>(bannerItemMapper)
                .eq(BannerItemDO::getBannerId,id)
                .list();*/
       return new BannerWithItemsBO(bannerDO,bannerItemDOList);
    }

    public void delete(Long id){
        BannerDO bannerDO = bannerMapper.selectById(id);
        if (bannerDO == null){
            new NotFoundException(20000);
        }
        this.delete(id);
    }
    public void update(BannerDTO bannerDTO, Long id){
        BannerDO bannerDO = bannerMapper.selectById(id);
        if (bannerDO == null){
            new NotFoundException(20000);
        }
        BeanUtils.copyProperties(bannerDTO,bannerDO);
        this.updateById(bannerDO);
    }
}
